<?php

namespace Module\Controller;

use Addons\Keywords\Keywords;
use Common\Common\CommonController;
use Module\Model\KeywordsModel;

class KeywordsController extends CommonController
{

    /**
     * 列出关键词
     */
    public function lists()
    {
        $data = M('keywords')->select();
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 关键字添加,编辑
     */
    public function  addkeyword()
    {
        if (isset($_GET['kid'])) {
            $kid=$_GET['kid'];
            $module=$_GET['module'];
            if(IS_POST){
                $this->nNull();
                $_POST['kid']=$kid;
                $_POST['module']=$module;
//                修改keywords表数据
               $res1= (new KeywordsModel())->add($_POST);
                //修改keycontents表数据
                $datas['keycontent']=$_POST['keyContent'];
                $datas['kid']=$kid;
                $datas['module']=$kid;
                $datas['edit']=1;
                $res2= (new Keywords())->keywordadd($datas);
                $res2=$res1 || $res1;
                $this->msg($res2,'编辑成功','编辑失败');
            }
            //此时是编辑
//            旧数据
           $keyword= M('keywords')->find($kid);
            $this->assign('keyword',$keyword);
            //在addoons中keywords模块中读出对应关键字的内容
            $con['kid']=$kid;
            $content = (new Keywords())->keywordadd($con);
            $this->assign('content', $content);
            $this->display();

        } else {
            if (IS_POST) {
               $this->nNull();
                //添加关键字表
                $data['keyword'] = I('post.keyword');
                $data['module'] = 'keywords';
                $kid = (new KeywordsModel())->add($data);

                //操作模块中的关键字内容表
                $contents['kid'] = $kid;
                $contents['module'] = 'keywords';
                $contents['keycontent'] = I('post.keyContent');
                $kcid = (new Keywords())->keywordadd($contents);
                $this->msg($kcid,'添加成功','添加失败');
            }
            //在addoons中keywords模块中读出对应关键字的内容
            $content = (new Keywords())->keywordadd();
            $this->assign('content', $content);
            $this->display();
        }
    }

    /**
     * 关键字删除
     *
     */
    public  function delkeyword(){
        $kid=$_GET['kid'];
//        dd($kid);die;

        $res=M('keywords')->where("kid=$kid")->delete();
       if($res){
          $res1=  M('keycontents')->where("kid=$kid")->delete();
       }
        $this->msg($res1,'删除成功','删除失败');
    }

//    非空判断
          private  function nNull(){
              if ($this->notnull(I('post.keyword'))) {
                  $this->error('关键字不能为空');
                  die;
              }
              if ($this->notnull(I('post.keyContent'))) {
                  $this->error('关键字回复内容不能为空');
                  die;
              }
          }

    //成功提示
    private function  msg($res,$msg1,$msg2){
        if ($res) {
            $this->success($msg1,__ROOT__.'/index.php/module/keywords/lists');
            die;
        } else {
            $this->error($msg2);
            die;
        }
    }

}